package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"question/models"
	"strconv"
	"time"
)

type QuestionController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *QuestionController) Ask() {
	title := c.GetString("title")
	author := c.GetString("author")
	img := c.GetString("img")
	cate_id, _ := c.GetInt("cate_id")

	question := models.Question{
		Title:  title,
		Author: author,
		Img:    img,
		CateId: uint(cate_id),
	}

	err := models.DB.Create(&question).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *QuestionController) QuestionType() {
	var question []models.Question
	err := models.DB.Find(&question).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "分类查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "分类查询成功",
			Data: question,
		}
		c.ServeJSON()
	}
}

func (c *QuestionController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	defer file.Close()
	path := "static/img/" + header.Filename
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件过大",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {

		Img := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "上传成功",
			Data: Img,
		}
		c.ServeJSON()
	}
}

func (c *QuestionController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)

	key := "list-" + strconv.Itoa(page)
	jsonData := make(map[string]interface{})
	query := models.DB.Model(&models.Question{})
	val, err := models.Rdb.Get(key).Result()

	if err != nil {
		var count int64
		err = query.Count(&count).Error
		offset := (page - 1) * size
		var question []models.Question
		err = query.Limit(size).Offset(offset).Find(&question).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			jsonData["question"] = question
			jsonData["count"] = count
			res, _ := json.Marshal(&jsonData)
			value := string(res)
			models.Rdb.Set(key, value, time.Second*30)

			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情展示成功-mysql",
				Data: jsonData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &jsonData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "详情展示成功-redis",
			Data: jsonData,
		}
		c.ServeJSON()
	}
}

func (c *QuestionController) Info() {
	id, _ := c.GetInt("id")
	key := "info:" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var question models.Question
	if err != nil {
		err = models.DB.Where("id=?", id).First(&question).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "详情展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			jsonByte, _ := json.Marshal(question)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Second*30)

			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情展示成功-mysql",
				Data: question,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &question)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "详情展示成功-redis",
			Data: question,
		}
		c.ServeJSON()
	}
}
